import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_edit_profile/lf_edit_profile_logic.dart';
import 'package:largefriends/pages/lf_payment/lf_payment_logic.dart';
import 'package:largefriends/pages/lf_profile/lf_profile_logic.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';

import '../../../net/lf_api_util.dart';
import '../../../res/lf_widget_ext.dart';
import '../../../widgets/common_failed_page.dart';
import '../../../widgets/user_card.dart';
import '../../lf_home/lf_home_logic.dart';
import '../contact_upgrade_banner.dart';
import '../lf_contact_empty.dart';
import '../lf_contact_list_loading.dart';
import '../lf_contact_sub_logic.dart';
import 'lf_admires_logic.dart';

class LfAdmiresPage extends StatefulWidget {
  const LfAdmiresPage({super.key});

  @override
  State<LfAdmiresPage> createState() => _LfAdmiresPageState();
}

class _LfAdmiresPageState extends State<LfAdmiresPage> {
  final logic = Get.put(LfAdmiresLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LfAdmiresLogic>(
      builder: (logic) {
        if (logic.dataList.isEmpty) {
          if (logic.state == LoadingState.loading ||
              logic.state == LoadingState.init) {
            return const LfContactListLoading();
          } else if (logic.state == LoadingState.failed) {
            return CommonFailedPage(
              retry: () {
                logic.getCustomParamHttpRequest(isRefresh: true);
              },
            );
          }
          var tips = strUpgradeToPremiumFeaturedTip.tr;
          var actionTips = strUpgrade.tr;
          var action = () {
            toPayment();
          };
          if (logic.authService.currentAccount!.isGold()) {
            if (logic.authService.currentAccount!.publicPhotos().length < 2 ||
                logic.authService.currentAccount!.headline.isNullOrEmpty ||
                logic.authService.currentAccount!.about.isNullOrEmpty ||
                logic.authService.currentAccount!.matchAbout.isNullOrEmpty) {
              tips = strCompleteProfileAdmiresTip.tr;
              actionTips = strCompleteProfile.tr.toUpperCase();
              action = () {
                toEditProfilePage();
              };
            } else {
              tips = strTakeInitiativeTip.tr;
              actionTips = strSearchMatches.tr;
              action = () {
                LfHomeLogic homeLogic = Get.find();
                homeLogic.pageController.jumpTo(0);
                homeLogic.selectedIndex.value = 0;
              };
            }
          }
          return LfContactEmptyWidget(
            title: strNoOneLikedTip.tr,
            content: tips,
            actionTitle: actionTips,
            action: action,
          );
        }
        return Padding(
          padding: EdgeInsets.only(top: 10.h),
          child: ScrollConfiguration(
            behavior: LfScrollBehavior(),
            child: EasyRefresh(
              controller: logic.refreshController,
              onLoad: () async {
                var result =
                    await logic.getCustomParamHttpRequest(isRefresh: false);
                return result;
              },
              onRefresh: () async {
                var result =
                    await logic.getCustomParamHttpRequest(isRefresh: true);
                return result;
              },
              child: CustomScrollView(
                slivers: [
                  logic.authService.currentAccount!.isGold()
                      ? empty.sliverBox
                      : getContactBanner(content: strUpgradeTip.tr).sliverBox,
                  SliverPadding(
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    sliver: SliverList.separated(
                      itemBuilder: (context, index) {
                        var userEntity = logic.dataList[index];
                        return InkWell(
                          child: UserCardWidget(
                            userEntity: userEntity,
                            showNew: true,
                            needBlur:
                                logic.authService.currentAccount!.isGold() ==
                                    false,
                          ),
                          onTap: () {
                            if (logic.authService.currentAccount!.isGold()) {
                              if (userEntity.isNew > 0) {
                                deleteNotificationBadge(
                                    uid: userEntity.userId!,
                                    type: BadgeChangeType.likedMe);
                              }
                              toProfilePage(
                                  uid: userEntity.userId!,
                                  userEntity: userEntity,
                                  type: ContactType.likeMe);
                            } else {
                              toPayment();
                            }
                          },
                          onLongPress: () {
                            if (logic.authService.currentAccount!.isGold()) {
                              logic.deleteLikedMe(userEntity);
                            }
                          },
                        );
                      },
                      separatorBuilder: (context, index) {
                        return 15.h.sbh;
                      },
                      itemCount: logic.dataList.length,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
